package org.jsola.contract.constant;

import org.jsola.core.validator.IConst;
import org.jsola.json.JsonKit;

import java.util.HashMap;
import java.util.Map;

/**
 * 个人身份证件类型
 *
 * @author JanusMix
 */
public enum PersonAreaType implements IConst {

    /**
     * 大陆身份证
     */
    MAINLAND("MAINLAND","大陆身份证"),
    /**
     * 香港居民往来内地通行证
     */
    HONGKONG("HONGKONG","香港居民往来内地通行证"),
    /**
     * 澳门居民往来内地通行证
     */
    MACAO("MACAO","澳门居民往来内地通行证"),
    /**
     * 台湾居民来往大陆通行证
     */
    TAIWAN("TAIWAN","台湾居民来往大陆通行证"),
    /**
     * 外籍
     */
    FOREIGN("FOREIGN","外籍"),
    /**
     * 护照
     */
    PASSPORT("PASSPORT","护照"),
    /**
     * 军人身份证
     */
    SOLDIER_IDNO("SOLDIER_IDNO","军人身份证"),
    /**
     * 社会保障卡
     */
    SOCIAL_SECURITY_CARD("SOCIAL_SECURITY_CARD","社会保障卡"),
    /**
     * 武装警察身份证件
     */
    ARMED_POLICE_ID("ARMED_POLICE_ID","武装警察身份证件"),
    /**
     * 户口簿
     */
    RESIDENCE_BOOKLET("RESIDENCE_BOOKLET","户口簿"),
    /**
     * 临时居民身份证
     */
    TEMPORARY_IDNO("TEMPORARY_IDNO","临时居民身份证"),
    /**
     * 外国人永久居留证
     */
    FOREIGNER_PERMANENT_RESIDENCE_PERMIT("FOREIGNER_PERMANENT_RESIDENCE_PERMIT","外国人永久居留证"),
    /**
     * 其他
     */
    OTHER("OTHER","其他");

    private final String value;
    private final String desc;
    /**
     * 枚举值罗列，给swagger接口文档展示用
     */
    public static final String VALUES_STR = "MAINLAND,HONGKONG,MACAO,TAIWAN,FOREIGN,PASSPORT,SOLDIER_IDNO,SOCIAL_SECURITY_CARD,ARMED_POLICE_ID,RESIDENCE_BOOKLET,TEMPORARY_IDNO,FOREIGNER_PERMANENT_RESIDENCE_PERMIT,OTHER";

     /**
     * 缓存所有value
     */
    private static final Map<String, PersonAreaType> LOOKUP = new HashMap<>();

    static {
        for (PersonAreaType e : PersonAreaType.values()) {
            LOOKUP.put(e.value, e);
        }
    }


    PersonAreaType(String value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    /**
     * 根据value查找PersonAreaType
     * @param value value
     * @return PersonAreaType
     */
    public static PersonAreaType find(String value) {
        return LOOKUP.get(value);
    }

    /**
     * 根据desc查找PersonAreaType
     * @param desc desc
     * @return PersonAreaType
     */
    public static PersonAreaType findByDesc(String desc){
        for (PersonAreaType e : PersonAreaType.values()) {
            if(e.getDesc().equals(desc)){
                return e;
            }
        }
        return null;
    }

    /**
     * 所有枚举转json
     * @return json
     */
    public static String getAllToJson() {
        Map<String,Object> map = new HashMap<>();
        for (PersonAreaType e : PersonAreaType.values()) {
            map.put("value", e.value);
            map.put("desc", e.desc);
        }
        return JsonKit.toJSONString(map);
    }

    @Override
    public boolean check(Object obj) {
        String value = (String) obj;
        return find(value) != null;
    }

    public String getValue() {
        return value;
    }

    public String getDesc() {
        return desc;
    }
}
